﻿using System;
using System.IO;

namespace Minecraft_Armor_Stand_Creater.Util
{
    enum LogType { Info, Error };//输出的日志类型

    class Log
    {
        //日志存储目录
        public static string LogPath = @".\Logs\";
        public static StreamWriter streamWriter;

        static Log()
        {
            if (!Directory.Exists(LogPath))
                Directory.CreateDirectory(LogPath);

            string file = $"{LogPath}{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}.txt";
            streamWriter = new(file, true);
        }

        /// <summary> 
        /// 输出日志信息 
        /// </summary>
        /// <param name="log"> 日志信息 </param>
        /// <param name="type"> 信息类型 </param>
        /// <param name="isFreeze"> 是否进行持久化存储。开启时写入".\Logs\[Date].txt"文件 </param>
        public static void Write(string log, LogType type, bool isFreeze = true)
        {
            string printLog = $"{GetDate()}--{GetTime()} [{type}] {log}";
            //Console.WriteLine(printLog);//控制台打印

            //写入日志文件
            if (isFreeze)
                WriteToFile(printLog);
        }

        /// <summary> 
        /// 写入字符串到文档
        /// </summary>
        /// <param name="text"> 字符串内容 </param>
        public static void WriteToFile(string text)
        {
            streamWriter.WriteLine(text);//追加文件末尾+换行
        }

        /// <summary>
        /// 获取日期
        /// </summary>
        /// <returns></returns>
        static string GetDate()
        {
            return $"[{DateTime.Now.Year}-{DateTime.Now.Month}-{DateTime.Now.Day}";
        }

        /// <summary>
        /// 获取时间
        /// </summary>
        /// <returns></returns>
        static string GetTime()
        {
            return $"{DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}.{DateTime.Now.Millisecond}";
        }
    }
}
